//
//  FlowLayout.m
//  UICollectionViewFlowLayoutTest
//
//  Created by Rocky on 16/4/12.
//  Copyright © 2016年 Rocky. All rights reserved.
//

#import "FlowLayout.h"
#import "ViewController.h"

#define ACTIVE_DISTANCE 100
#define ZOOM_FACTOR 0.3
#define ITEM_SIZE 100
//
@implementation FlowLayout

-(id)init
{
    self = [super init];
    if (self) {
        self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.sectionInset = UIEdgeInsetsMake(100, 0.0, 100, 0.0);//确定了缩进，此处为上方和下方各缩进200个point
        self.minimumLineSpacing = 50.0;//这个定义了每个item在水平方向上的最小间距。
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
}

//proposedContentOffset是没有对齐到网格时本来应该停下的位置
-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    CGFloat offsetAdjustment = MAXFLOAT;
    CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    NSArray* array = [super layoutAttributesForElementsInRect:targetRect];
    //对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较，找出最接近中心的一个
    for (UICollectionViewLayoutAttributes* layoutAttributes in array)
    {
        CGFloat itemHorizontalCenter = layoutAttributes.center.x;
        if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment))
        {
            offsetAdjustment = itemHorizontalCenter - horizontalCenter;
        }
    }
    //返回调整后停下来的位置
    return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
//返回 有效框中item属性
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect =CGRectMake(0, 0, 0, 0);
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;
    //
    for (UICollectionViewLayoutAttributes* attributes in array)
    {
        //检测item是否已与 可视窗口rect范围内？
        //对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较，找出最接近中心的一个
        if (CGRectIntersectsRect(attributes.frame, rect))
        {
            //计算出itme的中心X所在可视窗口中的位置。
            CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
            //如果itme的中心X在ACTIVE_DISTANCE 范围内进入设置
            if (ABS(distance) < ACTIVE_DISTANCE)
            {
                //计算出缩放比
                CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
                CGFloat zoom = 1 +(1 - ABS(normalizedDistance))*ZOOM_FACTOR;
                attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
                attributes.zIndex = 1;
                //attributes.transform =CGAffineTransformScale(attributes.transform, 1.5, 1.5);
            }
        }
    }
    return array;
}


//返回YES，这样当边界改变的时候，-invalidateLayout会自动被发送，才能让layout得到刷新。
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}
/*
-(CGSize)collectionViewContentSize
{
    NSLog(@"===collectionViewContentSize==");
    CGFloat height=  ceil([[self collectionView]  numberOfItemsInSection:0]/5)*SCREEN_WIDTH/2;
    return  CGSizeMake(SCREEN_WIDTH, height);
    
}*/
  /*
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *layoutAttributes = [NSMutableArray array];
  
    int firstItem = fmax(0, self.currentIndex - 2);
    int lastItem = fmin(self.cellCount-1 , self.currentIndex + 2);
    
    for (int i = firstItem;i <= lastItem; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [layoutAttributes addObject:attributes];
    }
   
    return [layoutAttributes copy];
} */
/*
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat index = indexPath.row;
    
    if (index < self.currentIndex)
    {
        attributes.zIndex = indexPath.item;
    } else if (index == self.currentIndex) {
        attributes.zIndex = 999;
    } else {
        attributes.zIndex = - indexPath.item;
    }
    
    attributes.size = self.itemSize;
    CGFloat offsetY = (indexPath.item - self.currentIndex)*10;
    attributes.center = CGPointMake(self.collectionView.bounds.size.width / 2,
                                    self.collectionView.bounds.size.height / 2
                                    + self.collectionView.contentOffset.y
                                    + offsetY);
    
    //0.0644 = 0.0322 * 2：310px scale to 300px
    CGFloat scaleFactor = 1 - (0.0644 * ABS(index - self.offset));
    CGAffineTransform scale = CGAffineTransformMakeScale(scaleFactor, 1);
    
    CGFloat translationFactor = self.offset - self.currentIndex;
    CGAffineTransform translation = CGAffineTransformMakeTranslation(0, translationFactor * -10 / 2);
    
    attributes.transform = CGAffineTransformConcat(scale, translation);
 
    return attributes;
}
*/
@end
